﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GameManager : MonoBehaviour
{
	public static int replayCount;
	public static DataSaveModule dataSave;
	public static GameManager instance;
	public static int currentLevel;
	public static GameState gameState;

	public GameManager ()
	{
		instance = this;
	}

	public static bool isNetworkConnected {
		get {
			return Application.internetReachability == NetworkReachability.NotReachable ? false : true;
		}
	}



	public static void LoadDataSave ()
	{
		Debug.Log ("load data coroutine");
		string str = PlayerPrefs.GetString ("");

		//str = "";
		bool isReplaceData = false;
		if (str != null && str != "") {
			//Debug.Log (str);
			try {
				dataSave = JsonUtility.FromJson<DataSaveModule> (str);
			} catch (System.Exception e) {
				isReplaceData = true;
				//Debug.Log ("Thay data : " + e);
				if (dataSave == null) {
					
					try {
						byte[] decodedStringToByte = System.Convert.FromBase64String (str);
						string decodedString = System.Text.Encoding.UTF8.GetString (decodedStringToByte);
						dataSave = JsonUtility.FromJson<DataSaveModule> (decodedString);

						string hash = PlayerPrefs.GetString ("Data_Hash");
						string hash_check = Md5Sum (decodedString + dataSave.levelMax.ToString ());
						// check hash to detect hack
						if (!hash.Equals (hash_check)) {
							Debug.Log ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa Phat hien nghi van hack");
							dataSave.levelMax = new SafeInt ((int)dataSave.numBlockInDayCount - (int)dataSave.todayDate);
							GameManager.SaveData ();
						}	
						SaveData ();
					} catch (System.Exception ee) {
						Debug.Log ("aaaaaaaaaaa tao moi data");
						dataSave.isMusicOn = true;
						dataSave.isSoundOn = true;
						dataSave = new DataSaveModule ();
						dataSave.date = System.DateTime.Now.Ticks;
						dataSave.levelMax = new SafeInt (1);
						dataSave.openGameCount = 0;
						SaveData ();
					}
				}
			}
			dataSave.openGameCount++;
			if (dataSave.date == 0) {
				dataSave.date = System.DateTime.Now.Ticks;
				SaveData ();
				Debug.Log ("data cu chua co date  , tao moi date");
			} else {
				if (isReplaceData == false) {
					string hash = PlayerPrefs.GetString ("Data_Hash");
					string hash_check = Md5Sum (str + dataSave.levelMax.ToString ());
					// check hash to detect hack
					if (!hash.Equals (hash_check)) {
						Debug.Log ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa Phat hien nghi van hack");
						dataSave.levelMax = new SafeInt ((int)(dataSave.numBlockInDayCount - dataSave.todayDate));
						GameManager.SaveData ();
					}	
				}
			}

		} else {
			dataSave = new DataSaveModule ();
			dataSave.date = System.DateTime.Now.Ticks;
			dataSave.levelMax = new SafeInt (1);
			dataSave.openGameCount = 0;
			dataSave.isMusicOn = true;
			dataSave.isSoundOn = true;
			SaveData ();
		}

	}






	public static void SaveData ()
	{
		dataSave.todayDate = System.DateTime.Now.Ticks;
		dataSave.numBlockInDayCount = dataSave.todayDate + dataSave.levelMax.GetValue ();
		string str = JsonUtility.ToJson (dataSave);
		byte[] toByteEncode = System.Text.Encoding.UTF8.GetBytes (str);
		string encodedText = System.Convert.ToBase64String (toByteEncode);
		//		Debug.Log ("str save" + str);
		PlayerPrefs.SetString ("Data_Hash", Md5Sum (str + dataSave.levelMax.ToString ()));
		PlayerPrefs.SetString ("fuckingHack", encodedText);
		PlayerPrefs.Save ();
	}





	public static string Md5Sum (string strToEncrypt)
	{
		System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding ();
		byte[] bytes = ue.GetBytes (strToEncrypt);

		// encrypt bytes
		System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider ();
		byte[] hashBytes = md5.ComputeHash (bytes);

		// Convert the encrypted bytes back to a string (base 16)
		string hashString = "";

		for (int i = 0; i < hashBytes.Length; i++) {
			hashString += System.Convert.ToString (hashBytes [i], 16).PadLeft (2, '0');
		}

		return hashString.PadLeft (32, '0');
	}
}

public enum GameState
{
	MENU,
	SELECT_LEVEL,
	PLAYING,
	PAUSING,
	WIN
}